# Copyright 2019 The Newbilling Authors. All rights reserved.
# Use of this source code is governed by a Apache license
# that can be found in the LICENSE file.

FROM dockerhub.qingcloud.com/qingbilling/nb-proto-builder as builder

ARG APP_NAME
ARG APP_VERSION
ARG BUILD_VERSION
ARG BUILD_TIME
ARG GIT_REVISION
ARG GIT_BRANCH
ARG GoVersion

WORKDIR /go/project

ENV CGO_ENABLED=0 GOOS=linux GOBIN=/project_bin GO111MODULE=on GOPROXY=https://goproxy.cn TZ=UTC

COPY . .

RUN mkdir -p /project_bin

RUN go build  -mod=vendor \
-ldflags "-w -s -X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.AppName=${APP_NAME}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.AppVersion=${APP_VERSION}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.BuildVersion=${BUILD_VERSION}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.BuildTime=${BUILD_TIME}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.GitRevision=${GIT_REVISION}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.GitBranch=${GIT_BRANCH}' \
-X 'git.internal.yunify.com/benchmark/benchmark/pkg/app.GoVersion=$(go version)'" \
-o /project_bin ./cmd/...

RUN find /project_bin -type f -exec upx {} \;

RUN mkdir /go/proudction


FROM alpine:3.12

ARG APP_VERSION
ENV TZ=UTC
WORKDIR /go/project_zip

RUN mkdir -p /go/project_zip_out

RUN apk add --update ca-certificates zip && update-ca-certificates

COPY --from=builder /project_bin/* /go/project_zip/cmd/
COPY ./configs /go/project_zip/configs
COPY ./migrates /go/project_zip/migrates
COPY ./scripts /go/project_zip/scripts

